package com.community.body.service.impl;

import java.util.Arrays;
import java.util.List;

import com.community.body.domain.ZyComplaintSuggest;
import com.community.body.dto.ComplaintSuggestDTO;
import com.community.body.mapper.ZyComplaintSuggestMapper;
import com.community.body.mapper.ZyFilesMapper;
import com.community.body.service.IZyComplaintSuggestService;
import com.community.body.vo.EchartsVo;
import com.community.body.service.IZyFilesService;
import com.community.common.utils.DateUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 投诉建议 Service业务层处理
 *
 * @author community
 * @date 2024-04-16
 */
@Service
public class ZyComplaintSuggestServiceImpl implements IZyComplaintSuggestService {
    @Resource
    private ZyComplaintSuggestMapper zyComplaintSuggestMapper;

    @Resource
    private RedisTemplate<String, Long> redisTemplate;
    @Resource
    private ZyFilesMapper zyFilesMapper;


    /**
     * 查询投诉建议
     *
     * @param complaintSuggestId 投诉建议 主键
     * @return 投诉建议
     */
    @Override
    public ZyComplaintSuggest selectZyComplaintSuggestByComplaintSuggestId(Long complaintSuggestId) {
        return zyComplaintSuggestMapper.selectZyComplaintSuggestByComplaintSuggestId(complaintSuggestId);
    }

    /**
     * 查询投诉建议 列表
     *
     * @param complaintSuggestDTO 投诉建议
     * @return 投诉建议
     */
    @Override
    public List<ComplaintSuggestDTO> selectZyComplaintSuggestList(ComplaintSuggestDTO complaintSuggestDTO) {
        if ("admin".equals(complaintSuggestDTO.getUserName()) && complaintSuggestDTO.getUserName() != null) {
            complaintSuggestDTO.setDeptId(0);
        }
        return zyComplaintSuggestMapper.selectZyComplaintSuggestList(complaintSuggestDTO);
    }

    /**
     * 新增投诉建议
     *
     * @param zyComplaintSuggest 投诉建议
     * @return 结果
     */
    @Override
    public int insertZyComplaintSuggest(ZyComplaintSuggest zyComplaintSuggest) {
        ValueOperations<String, Long> value = redisTemplate.opsForValue();
        ZyComplaintSuggest suggest = new ZyComplaintSuggest();
        suggest.setComplaintSuggestContent(zyComplaintSuggest.getComplaintSuggestContent());
        suggest.setCreateTime(DateUtils.getNowDate());
        suggest.setComplaintSuggestType(zyComplaintSuggest.getComplaintSuggestType());
        suggest.setStatus(0);
        suggest.setUserId(zyComplaintSuggest.getUserId());
        suggest.setCommunityId(zyComplaintSuggest.getCommunityId());
        int i = zyComplaintSuggestMapper.insertZyComplaintSuggest(suggest);
        List<ComplaintSuggestDTO> list = zyComplaintSuggestMapper.selectZyComplaintSuggestList1(suggest);
        value.set("complaintSuggestId", list.get(0).getComplaintSuggestId());
        return i;
    }

    /**
     * 修改投诉建议
     *
     * @param zyComplaintSuggest 投诉建议
     * @return 结果
     */
    @Override
    public int updateZyComplaintSuggest(ZyComplaintSuggest zyComplaintSuggest) {
        zyComplaintSuggest.setUpdateTime(DateUtils.getNowDate());
        return zyComplaintSuggestMapper.updateZyComplaintSuggest(zyComplaintSuggest);
    }

    /**
     * 批量删除投诉建议
     *
     * @param complaintSuggestIds 需要删除的投诉建议 主键
     * @return 结果
     */
    @Override
    public int deleteZyComplaintSuggestByComplaintSuggestIds(Long[] complaintSuggestIds) {
        return zyComplaintSuggestMapper.deleteZyComplaintSuggestByComplaintSuggestIds(complaintSuggestIds);
    }

    /**
     * 删除投诉建议 信息
     *
     * @param complaintSuggestId 投诉建议 主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteZyComplaintSuggestByComplaintSuggestId(Long complaintSuggestId) {
        int i = zyComplaintSuggestMapper.deleteZyComplaintSuggestByComplaintSuggestId(complaintSuggestId);
        int o = zyFilesMapper.deleteZyFilesByParentId(complaintSuggestId);
        return i+o > 0 ? 1:0;
    }

    @Override
    public List<EchartsVo> getStatusList(ComplaintSuggestDTO complaintSuggestDTO) {
        if (complaintSuggestDTO.getCreateBy() != null){
            String[] split = complaintSuggestDTO.getCreateBy().split(",");
            Long[] longArray = Arrays.stream(split)
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toArray(Long[]::new);
            return zyComplaintSuggestMapper.getStatusList1(longArray);
        }
        return zyComplaintSuggestMapper.getStatusList(complaintSuggestDTO);
    }
}
